285B - Find Marble - CodeForces Solution


implementation *1200

Please click on ads to support us..

Python Code:

import math
import sys

n,s,t=map(int,input().split())

    
        arr=list(map(int,input().split()))
if(s==t):
    print(0)
    sys.exit()
i=0
while(arr[s-1]!=t and i<=n-1):
    s=arr[s-1]
    
    i+=1
if(arr[s-1]==t):

    print(i+1)
else:
    print(-1)

C++ Code:

#include <bits/stdc++.h>
using ll = long long;
#define Shalaby ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
using namespace std;
#define endl '\n'
#define Endl '\n'
#define YES cout << "YES\n";
#define NO cout << "NO\n";
#define nl cout << '\n';
#define all(v) v.begin(), v.end()
#define loop(a)       \
    for (auto &i : a) \
        cin >> i;
ll mod = 1e9 + 7;
bool isprime(ll n)
{
    for (int i = 2; i * i < n; i++)
    {
        if (n % i == 0)
            return false;
    }
    return true;
}
bool com(pair<int, int> s1, pair<int, int> s2)
{
    return s1.second < s2.second;
}
bool palindrom(string s)
{
    for (int i = 0; i < s.size() / 2; i++)
    {
        if (s[i] != s[s.size() - i - 1])
        {
            return false;
        }
    }
    return true;
}
vector<int> prime_factorization(ll n)
{
    vector<int> pf;
    for (int i = 2; i <= sqrt(n); i++)
    {
        while (n % i == 0)
        {
            pf.push_back(i);
            n /= i;
        }
    }
    if (n > 1)
        pf.push_back(n);
    return pf;
}
int searchInsert(vector<int> &nums, int target)
{
    int n = 0, m = nums.size() - 1, mid;
    while (n <= m)
    {
        mid = (n + m) / 2;
        cout << mid << " " << nums[mid] << endl;
        ;
        if (nums[mid] == target)
        {
            break;
        }
        else if (nums[mid] < target)
        {
            n = mid + 1;
        }
        else
            m = mid - 1;
    }
    return mid;
}
ll factorial(ll n)
{
    ll mul = 1;
    for (ll i = n; i <= n; i++)
    {
        mul *= i;
    }
    return mul;
}
// moduler binary exponention
ll Binary_Exponention(ll a, ll b, ll k) // fast power
{
    ll res = 1;
    while (b)
    {
        if (b % 2 == 1)
            res = ((res % mod) * (a % mod)) % mod;
        // a = Binary_Exponention(b, k);
        b /= 2;
    }
    return res;
}
ll no_zeros(ll n)
{
    ll ans = 0, ten = 1;
    while (n)
    {
        if (n % 10 != 0)
        {
            ans += ten * (n % 10);
            ten *= 10;
        }
        n /= 10;
    }
    return ans;
}
ll lcm(ll a, ll b)
{
    return (a / __gcd(a, b)) * b;
}
int divisorSum(int n)
{
    int sum = 0;

    for (int i = 1; i <= n; ++i)
    {

        // Find all divisors of i and add them
        for (int j = 1; j * j <= i; ++j)
        {
            if (i % j == 0)
            {
                if (i / j == j)
                    sum += j;
                else
                    sum += j + i / j;
            }
        }
    }
    return sum;
}
bool beaty(string s)
{
    for (int i = 1; i < s.size(); i++)
    {
        if (s[i] == s[i - 1])
            return false;
    }
    return true;
}
void solve()
{
    int n, s, t;
    cin >> n >> s >> t;
    int a[n], b[n + 1];
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
    }
    int ans = a[s-1];
    for (int i = 0; i < n; i++)
    {
        if (s == t)
        {
            cout << i << endl;
            return;
        }
        s = a[s-1];
    }
    cout << -1;
}
int main()
{
    Shalaby;
    int n = 1;
    // cin >> n;
    while (n--)
    {
        solve();
    }
}
/*

*/


Comments

Submit
0 Comments
More Questions

969. Pancake Sorting
967. Numbers With Same Consecutive Differences
957. Prison Cells After N Days
946. Validate Stack Sequences
921. Minimum Add to Make Parentheses Valid
881. Boats to Save People
497. Random Point in Non-overlapping Rectangles
528. Random Pick with Weight
470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST
445. Add Two Numbers II
442. Find All Duplicates in an Array
437. Path Sum III
436. Find Right Interval
435. Non-overlapping Intervals